<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://wapui.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Rifty <rifty@wapui.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Db;

class Order extends Base
{
    // 未处理订单
    public function order_list()
    {

        $list = Db::name('orders')
                    ->where('status', 0)
                    ->order('id desc')
                    ->paginate(100);   

        $this->assign('list', $list);
        return $this->fetch();
    }
    // 已处理订单
    public function order_ok()
    {

        $list = Db::name('orders')
                    ->where('status', 1)
                    ->order('id desc')
                    ->paginate(100);   

        $this->assign('list', $list);
        return $this->fetch();
    }

    public function order_shenhe(){
        $id = $this->request->param('id','0');
        Db::name('orders')->where('id',$id)->setField('status', 1);
        $this->success('操作成功');
    }
    public function order_shenhe2(){
        $id = $this->request->param('id','0');
        Db::name('orders')->where('id',$id)->setField('status', 0);
        $this->success('操作成功');
    }

    public function order_edit(){
        $id = $this->request->param('id','0');
        if(empty($id)) {
            $this->error('订单不存在');
        }

        $order = Db::name('orders')->where('id', $id)->find();
        if(empty($order)) {
            $this->error('订单不存在');
        }

        if(!empty($order['items'])) {
            $order['items']=explode('<br>',$order['items']); 
        }

        // dump($order);
        $this->assign('order', $order);
        return $this->fetch();
    }


    public function order_do_edit(){
        if (!$this->request->isPost()) {
            $this->error('错误的请求方式');
        }

        $id = $this->request->param('id','0');
        if(empty($id)) {
            $this->error('订单不存在');
        }

        $order = Db::name('orders')->where('id', $id)->find();
        if(empty($order)) {
            $this->error('订单不存在');
        }

        $name = $this->request->param('name','');
        if(empty($name)) {
            $this->error('收件人不能为空');
        }
        $city = $this->request->param('city','');
        if(empty($city)) {
            $this->error('省市区不能为空');
        }
        $address = $this->request->param('address','');
        if(empty($address)) {
            $this->error('地址不能为空');
        }
        $tel = $this->request->param('tel','');
        if(empty($tel)) {
            $this->error('手机号不能为空');
        }

        $checkbox1 = $this->request->param('checkbox1/a','');
        if(empty($checkbox1)) {
            $this->error('套餐至少选择一项');
        }


        $data['name'] = $name;
        $data['city'] = $city;
        $data['address'] = $address;
        $data['mobile'] = $tel;
        $data['items'] = implode('<br>',$checkbox1);
        Db::name('orders')->where('id', $id)->update($data);


        $this->success('添加成功');


    }

    public function order_delete(){
        $id = $this->request->param('id','0');
        if(empty($id)) {
            $this->error('ID不能为空');
        }

        $result = Db::name('orders')->delete($id);
        if(false !== $result){
            $this->success(lang('DELETE_SUCCESS'));
        }else{
            $this->error(lang('DELETE_FAILED'));
        }
    }

    public function exp_data(){//导出Excel
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
         header('Expires: 0');
         header('Cache-control: private');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Content-Description: File Transfer');
         header('Content-Encoding: UTF-8');
         // header('Content-type: text/csv; charset=UTF-8');
         header('Content-Disposition: attachment; filename=order_data.csv');
        echo chr(0xEF).chr(0xBB).chr(0xBF); 
        $data = Db::name('orders')
                    ->order('id desc')
                    ->select();

        $xlsCell = array(
            '收件人','地址','手机号','套餐','IP','订单时间'
        );

        $title= implode(",", $xlsCell);
        echo "$title\n";

        foreach ($data as $key => $value) {
            // $works_data[$key][0] = $value['id'];
            $works_data[$key][1] = $value['name'];
            $works_data[$key][2] = $value['city'] . ' ' . $value['address'];
            // $works_data[$key][3] = $value['address'];
            $works_data[$key][3] = $value['mobile'];
            $works_data[$key][4] = str_replace("<br>","，",$value['items']);;
            $works_data[$key][5] = $value['user_ip'];
            $works_data[$key][6] = date("Y-m-d H:i:s",$value['create_time']);

            $works_data[$key]=implode(",", $works_data[$key]);
        }
        exit(implode("\n",$works_data));
    }


}
